<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Template for \Magento\Backend\Block\Widget\Grid
 *
 *  getId()
 *  getCollection()
 *  getColumns()
 *  getPagerVisibility()
 *  getVarNamePage()
 *
 */
/* @var $this \Magento\Backend\Block\Widget\Grid */
$numColumns = sizeof($this->getColumns());
?>
<?php if ($this->getCollection()): ?>
<?php if ($this->canDisplayContainer()): ?>
    <?php if ($this->getGridHeader()): ?>
        <div class="page-title">
            <div class="page-title-inner">
                <h1 class="title"><?php echo $this->getGridHeader(); ?></h1>
            </div>
        </div>
    <?php endif ?>

    <div id="<?php echo $this->getId() ?>" data-grid-id="<?php echo $this->getId() ?>">
        <?php else: ?>
            <?php echo $this->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
        <?php endif; ?>
        <?php if ($this->getPagerVisibility() || $this->getExportTypes() || $this->getChildBlock('grid.columnSet')->getFilterVisibility()): ?>
            <div class="grid-actions">
                <?php if ($this->getChildBlock('grid.export')): ?>
                    <div class="export">
                        <?php echo $this->getChildHtml('grid.export'); ?>
                    </div>
                <?php endif; ?>
                <div class="filter-actions">
                    <?php echo $this->getMainButtonsHtml() ?>
                </div>
                <?php if ($this->getPagerVisibility()): ?>
                    <div class="pager">
                        <?php $countRecords = $this->getCollection()->getSize(); ?>
                        <span class="pages-total-found"><?php echo __('Total %1 records found', $countRecords) ?></span>
                        <span id="<?php echo $this->getHtmlId() ?>-total-count"
                              class="total-count no-display"<?php echo $this->getUiId('total-count') ?>>
                            <?php echo $countRecords ?>
                        </span>
                        <label class="view-pages">
                            <?php echo __('View') ?>
                            <select name="<?php echo $this->getVarNameLimit() ?>"
                                    onchange="<?php echo $this->getJsObjectName() ?>.loadByElement(this)" <?php echo $this->getUiId('per-page') ?>
                                    class="select">
                                <option value="20"<?php if ($this->getCollection()->getPageSize() == 20): ?>
                                    selected="selected"<?php endif; ?>>20
                                </option>
                                <option value="30"<?php if ($this->getCollection()->getPageSize() == 30): ?>
                                    selected="selected"<?php endif; ?>>30
                                </option>
                                <option value="50"<?php if ($this->getCollection()->getPageSize() == 50): ?>
                                    selected="selected"<?php endif; ?>>50
                                </option>
                                <option value="100"<?php if ($this->getCollection()->getPageSize() == 100): ?>
                                    selected="selected"<?php endif; ?>>100
                                </option>
                                <option value="200"<?php if ($this->getCollection()->getPageSize() == 200): ?>
                                    selected="selected"<?php endif; ?>>200
                                </option>
                            </select>
                            <?php echo __('per page') ?>
                        </label>
                    </div>
                <?php endif ?>
            </div>
        <?php endif; ?>
        <?php if ($this->getChildBlock('grid.massaction') && $this->getChildBlock('grid.massaction')->isAvailable()): ?>
            <?php echo $this->getChildHtml('grid.massaction') ?>
        <?php endif ?>

        <?php if ($this->getGridCssClass()): ?>
            <div class="grid-container">
                <table class="<?php echo $this->getGridCssClass() ?>" id="<?php echo $this->getId() ?>_table">
                    <!-- Rendering column set -->
                    <?php echo $this->getChildHtml('grid.columnSet'); ?>
                </table>
            </div>
        <?php else: ?>
            <div class="grid">
                <div class="hor-scroll">
                    <table class="data" id="<?php echo $this->getId() ?>_table">
                        <!-- Rendering column set -->
                        <?php echo $this->getChildHtml('grid.columnSet'); ?>
                    </table>
                </div>
                <?php if ($this->getPagerVisibility()): ?>
                    <div class="pager">
                        <label class="page"><?php echo __('Page') ?></label>

                        <?php $_curPage = $this->getCollection()->getCurPage() ?>
                        <?php $_lastPage = $this->getCollection()->getLastPageNumber() ?>

                        <?php if ($_curPage > 1): ?>
                            <a href="#" title="<?php echo __('Previous page') ?>" class="action-previous"
                               onclick="<?php echo $this->getJsObjectName() ?>.setPage('<?php echo($_curPage - 1) ?>');return false;"><span><?php echo __('Previous page') ?></span></a>
                        <?php else: ?>
                            <span class="action-previous disabled"><span><?php echo __('Previous page') ?></span></span>
                        <?php endif; ?>

                        <input type="text" name="<?php echo $this->getVarNamePage() ?>" value="<?php echo $_curPage ?>"
                               class="input-text page"
                               onkeypress="<?php echo $this->getJsObjectName() ?>.inputPage(event, '<?php echo $_lastPage ?>')" <?php echo $this->getUiId('current-page') ?> />

                        <span class="pages-total">
                            <?php echo __('of %1', $this->getCollection()->getLastPageNumber()) ?></span>
                        </span>

                        <?php if ($_curPage < $_lastPage): ?>
                            <a href="#" title="<?php echo __('Next page') ?>" class="action-next"
                               onclick="<?php echo $this->getJsObjectName() ?>.setPage('<?php echo($_curPage + 1) ?>');return false;"><span><?php echo __('Next page') ?></span></a>
                        <?php else: ?>
                            <span class="action-next disabled"><span><?php echo __('Next page') ?></span></span>
                        <?php endif; ?>
                        <?php if ($this->getChildBlock('grid.bottom.links')): ?>
                            <?php echo $this->getChildHtml('grid.bottom.links') ?>
                        <?php endif; ?>
                    </div>
                <?php endif ?>
            </div>
        <?php endif ?>
        <?php if ($this->canDisplayContainer()): ?>
    </div>
    <script type="text/javascript">
        var deps = ['mage/adminhtml/grid'];

    <?php if (strpos($this->getRowClickCallback(), 'order.')!==false): ?>
    deps.push('Magento_Sales/order/create/form')
    <?php endif; ?>

        require(deps, function(){
            <?php //TODO: getJsObjectName and getRowClickCallback has unexpected behavior. Should be removed ?>

            <?php echo $this->getJsObjectName() ?> = new varienGrid('<?php echo $this->getId() ?>', '<?php echo $this->getGridUrl() ?>', '<?php echo $this->getVarNamePage() ?>', '<?php echo $this->getVarNameSort() ?>', '<?php echo $this->getVarNameDir() ?>', '<?php echo $this->getVarNameFilter() ?>');
            <?php echo $this->getJsObjectName() ?>.useAjax = <?php echo $this->getUseAjax() ? 'true' : 'false' ?>;
            <?php if ($this->getRowClickCallback()): ?>
            <?php echo $this->getJsObjectName() ?>.rowClickCallback = <?php echo $this->getRowClickCallback() ?>;
            <?php endif; ?>
            <?php if ($this->getCheckboxCheckCallback()): ?>
            <?php echo $this->getJsObjectName() ?>.checkboxCheckCallback = <?php echo $this->getCheckboxCheckCallback() ?>;
            <?php endif; ?>
            <?php if ($this->getSortableUpdateCallback()): ?>
            <?php echo $this->getJsObjectName() ?>.sortableUpdateCallback = <?php echo $this->getSortableUpdateCallback()?>;
            <?php endif; ?>
            <?php echo $this->getJsObjectName() ?>.bindSortable();
            <?php if ($this->getRowInitCallback()): ?>
            <?php echo $this->getJsObjectName() ?>.initRowCallback = <?php echo $this->getRowInitCallback() ?>;
            <?php echo $this->getJsObjectName() ?>.initGridRows();
            <?php endif; ?>
            <?php if ($this->getChildBlock('grid.massaction') && $this->getChildBlock('grid.massaction')->isAvailable()): ?>
            <?php echo $this->getChildBlock('grid.massaction')->getJavaScript() ?>
            <?php endif ?>
            <?php echo $this->getAdditionalJavaScript(); ?>
        });
</script>
<?php endif; ?>
    <?php if ($this->getChildBlock('grid.js')): ?>
        <?php echo $this->getChildHtml('grid.js'); ?>
    <?php endif; ?>
<?php endif ?>
